package cn.btks.resultintegration.achievements.controller;

import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.util.JwtUtil;
import cn.btks.commonserver.valid.SystemLog;
import cn.btks.resultintegration.achievements.dto.CatalogIdListQueryParamDto;
import cn.btks.resultintegration.achievements.pojo.EnvironmentAttribute;
import cn.btks.resultintegration.achievements.service.EnvironmentAttributeService;
import cn.btks.resultintegration.feign.FeignObject.Unit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2021/1/28 10:25
 */
@Slf4j
@RestController
@RequestMapping("api/v1.0/resultIntegration/environmentAttribute")
@Api(tags = "环境单元要素模块")
@Validated
public class EnvironmentAttributeController {

    @Autowired
    EnvironmentAttributeService environmentAttributeService;

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询环境单元要素信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "环境单元要素ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id") Long id) {
        EnvironmentAttribute environmentAttribute = environmentAttributeService.findById(id);
        return environmentAttribute != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), environmentAttribute) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/findPages")
    @ApiOperation("分页查询环境单元要素信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "环境要素管控分区编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "环境要素管控分区名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cityName", value = "师名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "目录ID", required = false, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPages(Integer pageNumber, Integer pageSize, String code, String name, String cityName, Long catalogId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    return environmentAttributeService.findPage(pageNumber, pageSize, code,name,cityName,catalogId);
                }else {
                    cityName = unit.getCityName();
                    return environmentAttributeService.findPage(pageNumber, pageSize, code,name,cityName,catalogId);
                }
            }
        }
        return new ResultState(StateInfo.TOKEN_ERROR.getCode(), StateInfo.TOKEN_ERROR.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/findPages1")
    @ApiOperation("分页查询环境单元要素信息(某个师的数据)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "环境要素管控分区编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "环境要素管控分区名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "目录ID", required = false, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "师ID", required = false, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPages(Integer pageNumber, Integer pageSize, String code, String name, Long catalogId,Long cityId) {
        return environmentAttributeService.findPage(pageNumber, pageSize, code,name,catalogId,cityId);
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping("/findPages2")
    @ApiOperation("分页查询多目录下的环境单元要素信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "catalogIdList", value = "目录ID列表", required = false, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPages(@RequestBody CatalogIdListQueryParamDto catalogIdList) {
        return environmentAttributeService.findPage(catalogIdList.getPageNumber(), catalogIdList.getPageSize(), catalogIdList.getCatalogIdList());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping("/findAllByCatalogIdList")
    @ApiOperation("查询多目录下的环境单元要素信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "catalogIdList", value = "目录ID列表", required = false, dataType = "list"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findAllByCatalogIdList(@RequestBody CatalogIdListQueryParamDto catalogIdList,HttpServletRequest request) throws Exception {
        return environmentAttributeService.findAllByCatalogIdList(catalogIdList.getCatalogIdList(), request);
    }

    @SystemLog(type = SystemLogEnum.DOWNLOAD_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @GetMapping("/export")
    @ApiOperation("导出选中目录的环境单元或要素信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "环境要素管控分区编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "环境要素管控分区名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "cityName", value = "师名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "目录ID", required = false, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState export(String code,String name,String cityName,Long catalogId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    return environmentAttributeService.export(code, name,cityName,catalogId,request);
                }else {
                    cityName = unit.getCityName();
                    return environmentAttributeService.export(code, name,cityName,catalogId,request);
                }
            }
        }
        return new ResultState(StateInfo.TOKEN_ERROR.getCode(), StateInfo.TOKEN_ERROR.getMsg());
    }
}
